<?php
namespace Models\Model;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\Sql\Predicate;
use Zend\Db\Sql\Expression;
class UserTable
{
    protected $tableGateway;
	protected $select;
    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
		$this->select = new Select();
    }
	public function saveUserData($users)
    {
		$password=md5($users['password']);
		$data = array(
			'user_name' 	  	       => $users['inputFirstname'],		//.' '.$users['inputLastname']		
			'email' 		           => $users['inputEmail'],  		
			'password' 		           => $password,		
			'status'  	               => 1,  	
			'date_added' 	           => date('Y-m-d H:i:s'),   
			'date_updated'	  	       => date('Y-m-d H:i:s'), 	
			'user_type_id' 		       => $users['type_id'], 			
		);	
		$insertresult=$this->tableGateway->insert($data);	
		return $this->tableGateway->lastInsertValue;		
    }	
	public function saveshopData($shop)
    {
		$password=md5($shop['password']);
		$data = array(
			'user_name' 	  	       => $shop['inputFirstname'],		//.' '.$users['inputLastname']		
			'email' 		           => $shop['inputEmail'],  		
			'password' 		           => $password,		
			'status'  	               => 2,  	
			'date_added' 	           => date('Y-m-d H:i:s'),   
			'date_updated'	  	       => date('Y-m-d H:i:s'), 	
			'user_type_id' 		       => 3, 			
		);	
		$insertresult=$this->tableGateway->insert($data);	
		return $this->tableGateway->lastInsertValue;		
    }	
	public function checkDetails($details)
    {	
		$select = $this->tableGateway->getSql()->select();
		$select->join('user_details', new Expression('user_details.user_id=user.user_id'),array('*'),'left');
		$select->where('email="'.$details['email'].'"');
		$select->where('password="'.md5($details['pwd']).'"');
		if($details['user_type_id']!=1){
			$select->where('user.status=1');
		}
		if(isset($details['user_type_id']) && $details['user_type_id']!='')
		{	
		   $select->where('user_type_id="'.$details['user_type_id'].'"');
		}
		$resultSet = $this->tableGateway->selectWith($select);	
		return $resultSet;		
	}
	public function getpassword($userid,$pwdd){ 
		$pwd=md5($pwdd);
		$select = $this->tableGateway->getSql()->select();
		$select->where('password="'.$pwd.'"');
		$select->where('user_id="'.$userid.'"');
		$resultSet = $this->tableGateway->selectWith($select);			
		return $resultSet;
	}
	public function userdetails($id)
    {	
		$select = $this->tableGateway->getSql()->select();
		$select->where('user_id="'.$id.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet->current();
	}
	public function getUsersReport($data)
	{
		$fdate = date_create($data['from_date']);
		$from_date=date_format($fdate, 'Y-m-d');
		$tdate = date_create($data['to_date']);
		$to_date=date_format($tdate, 'Y-m-d');
		$select = $this->tableGateway->getSql()->select();
		//if(isset($data['current_page']) && $data['current_page']!='')
		//{
		//	$currentPage=$data['current_page'];
		//}
		//else{
		//$currentPage='1';
		//}
		///if(isset($data['results_per_page']) && $data['results_per_page']!='')
		//{
		//	$resultsPerPage=$data['results_per_page'];
		//}
		//else{
		//$resultsPerPage='10';
		//}
		//$limit=$resultsPerPage;
		//$offset=($currentPage-1)*$resultsPerPage;
		if((isset($from_date) && $from_date!='') && (isset($to_date) && $to_date!='')){
		$select->where('date_added>="'.$from_date.'"');
		$select->where('date_added<="'.$to_date.'"');
		}
		$select->order('user_id DESC');
		$resultSet = $this->tableGateway->selectWith($select);
		
		return $resultSet;
	}
	public function checkUniqueRecord($email){        
		$select = $this->tableGateway->getSql()->select()                        
			   ->where('email="'.$email.'"');                                         
	   $resultSet = $this->tableGateway->selectWith($select);                            
	   return $resultSet;
	} 
   
	public function checkloginEmail($data){     
		$select = $this->tableGateway->getSql()->select();                        
			   $select->where('email="'.$data['email'].'"');                                         
			   $select->where('user_type_id="'.$data['user_type_id'].'"');                                         
	   $resultSet = $this->tableGateway->selectWith($select);                            
	   return $resultSet;
	} 
	public function changepwd($userid,$pwd){
	   $password=md5($pwd);
	   $data = array(
		   'password'      =>$password,
		);
	   $changepassword=$this->tableGateway->update($data, array('user_id' => $userid));	   
	   return $changepassword;                        
   }
   public function updateUserRegAuth($userid){
	   $data = array(
			'status'          =>'1',
	   );
	   $updateuserid=$this->tableGateway->update($data, array('user_id' => $userid));
	   return $updateuserid;                        
	}
	public function updateUserData($users,$uid)
    {
			$data = array(
				'user_name' 	  	       => $users['user_name'], 				
				'status'  	               => $users['status'],  	
				'date_added' 	           => date('Y-m-d H:i:s'),   
				'date_updated'	  	       => date('Y-m-d H:i:s'), 
			);	
			$updateuserid=$this->tableGateway->update($data, array('user_id' => $uid));
			return $updateuserid;
    }
	public function getUserData($user_id){        
		$select = $this->tableGateway->getSql()->select();
		$select->join('user_details', new Expression('user_details.user_id=user.user_id'),array('landmark','address','phone','alterphone','location','pincode','phone','fax','detailed_status'=>'status'),'left');	
		$select->where('user.user_id="'.$user_id.'"');                                         
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;
    }	
	public function getShopsByFilter($data)
	{
		$filters=array();
		if(isset($data['pincode']) && $data['pincode']!=''){
		$filters['pincode']=$data['pincode'];
		}
		if(isset($data['location']) && $data['location']!=''){
		$filters['location']=$data['location'];
		}
		if(isset($data['type_id']) && $data['type_id']!=''){
		$filters['user_type_id']=$data['type_id'];
		}
		if(isset($data['status']) && $data['status']!=''){
		$filters['status']=$data['status'];
		}
		$select = $this->tableGateway->getSql()->select();
		$select->join('user_details', new Expression('user_details.user_id=user.user_id'),array('landmark','address','phone','alterphone','location','pincode','phone','fax','detailed_status'=>'status'),'left');	
		if(count($filters))
		{
			foreach($filters as $key=>$searchby)
			{
				if($searchby!='0')
				{
					if($key=='user_type_id')
					{
						$select->where('user.'.$key.'="'.$searchby.'"');
						if($filters['status']==2){
							$select->where('user.status="2"');
						}else{
							$select->where('user.status!="2"');
						}
						
					}//else{
						//$select->where('user_details.'.$key.'="'.$searchby.'"');
				//	}
				}
			}
		}
		$select->order('date_added desc');
		$resultSet = $this->tableGateway->selectWith($select);	
		return $resultSet;
	}
	public function Totalcount($data)
	{	
		$filters=array();
		if(isset($data['pincode']) && $data['pincode']!=''){
		$filters['pincode']=$data['pincode'];
		}
		if(isset($data['location']) && $data['location']!=''){
		$filters['location']=$data['location'];
		}
		if(isset($data['type_id']) && $data['type_id']!=''){
		$filters['user_type_id']=$data['type_id'];
		}
		if(isset($data['status']) && $data['status']!=''){
		$filters['status']=$data['status'];
		}
		$select = $this->tableGateway->getSql()->select();
		$select->join('user_details', new Expression('user_details.user_id=user.user_id'),array('landmark','address','phone','alterphone','location','pincode','phone','fax','detailed_status'=>'status'),'left');	
		if(count($filters))
		{
			foreach($filters as $key=>$searchby)
			{
				if($searchby!='0')
				{
					if($key=='user_type_id')
					{
						$select->where('user.'.$key.'="'.$searchby.'"');
						if($filters['status']==2){
							$select->where('user.status="2"');
						}else{
							$select->where('user.status!="2"');
						}
						
					}//else{
						//$select->where('user_details.'.$key.'="'.$searchby.'"');
				//	}
				}
			}
		}
		$resultSet = $this->tableGateway->selectWith($select);	
		return $resultSet->count();
	}
	public function changeUserStatus($uid,$status)
    {
		$data = array(
			'status' 	  	       	   => $status['status'], 				
			'date_updated'	  	       => date('Y-m-d H:i:s'), 	
		);	
		$updateuserid=$this->tableGateway->update($data, array('user_id' => $uid));
		return $updateuserid;	
    }
	public function cntShopsAndUsers($type){
		$select = $this->tableGateway->getSql()->select();   
		if($type=='shop_users'){
			$select->where('user_type_id="3"');   
		}else if($type=='users'){
			$select->where('user_type_id="2"');  
		}	
		$select->where('status="1"');  	
	   $resultSet = $this->tableGateway->selectWith($select);                            
	   return $resultSet->count();
	}
}